package life.majiang.community.controller;

import life.majiang.community.dto.CommentDTO;
import life.majiang.community.dto.QuestionDTO;
import life.majiang.community.enums.CommentTypeEnum;
import life.majiang.community.service.CommentService;
import life.majiang.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class QuestionControlle {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;


    @GetMapping("/question/{id}")
    public String question(@PathVariable(name = "id") Long id, Model model){

        /*获取单条问题*/
        QuestionDTO questionDTO = questionService.getById(id);
        List<CommentDTO> comments = commentService.listByTargetId(id, CommentTypeEnum.QUESTION );
        /*获取tag*/
        List<QuestionDTO> relateQuestions = questionService.selectRelated(questionDTO);

        /*累加阅读时*/
        questionService.inVive(id);

        /*获取问题*/
        model.addAttribute("question",questionDTO);
        /*评论*/
        model.addAttribute("comments",comments);
        /*tag标签*/
        model.addAttribute("relateQuestions",relateQuestions);

        return "question";
    }
}
